<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>define_peephole (GNU Compiler Collection (GCC) Internals)</title>

<meta name="description" content="define_peephole (GNU Compiler Collection (GCC) Internals)">
<meta name="keywords" content="define_peephole (GNU Compiler Collection (GCC) Internals)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Option-Index.html" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Peephole-Definitions.html" rel="up" title="Peephole Definitions">
<link href="define_005fpeephole2.html" rel="next" title="define_peephole2">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en">
<div class="subsection-level-extent" id="define_005fpeephole">
<div class="nav-panel">
<p>
Next: <a href="define_005fpeephole2.html" accesskey="n" rel="next">RTL to RTL Peephole Optimizers</a>, Up: <a href="Peephole-Definitions.html" accesskey="u" rel="up">Machine-Specific Peephole Optimizers</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="RTL-to-Text-Peephole-Optimizers"><span>17.18.1 RTL to Text Peephole Optimizers<a class="copiable-link" href="#RTL-to-Text-Peephole-Optimizers"> &para;</a></span></h4>
<a class="index-entry-id" id="index-define_005fpeephole"></a>

<p>A definition looks like this:
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_peephole
  [<var class="var">insn-pattern-1</var>
   <var class="var">insn-pattern-2</var>
   ...]
  &quot;<var class="var">condition</var>&quot;
  &quot;<var class="var">template</var>&quot;
  &quot;<var class="var">optional-insn-attributes</var>&quot;)
</pre></div>

<p>The last string operand may be omitted if you are not using any
machine-specific information in this machine description.  If present,
it must obey the same rules as in a <code class="code">define_insn</code>.
</p>
<p>In this skeleton, <var class="var">insn-pattern-1</var> and so on are patterns to match
consecutive insns.  The optimization applies to a sequence of insns when
<var class="var">insn-pattern-1</var> matches the first one, <var class="var">insn-pattern-2</var> matches
the next, and so on.
</p>
<p>Each of the insns matched by a peephole must also match a
<code class="code">define_insn</code>.  Peepholes are checked only at the last stage just
before code generation, and only optionally.  Therefore, any insn which
would match a peephole but no <code class="code">define_insn</code> will cause a crash in code
generation in an unoptimized compilation, or at various optimization
stages.
</p>
<p>The operands of the insns are matched with <code class="code">match_operands</code>,
<code class="code">match_operator</code>, and <code class="code">match_dup</code>, as usual.  What is not
usual is that the operand numbers apply to all the insn patterns in the
definition.  So, you can check for identical operands in two insns by
using <code class="code">match_operand</code> in one insn and <code class="code">match_dup</code> in the
other.
</p>
<p>The operand constraints used in <code class="code">match_operand</code> patterns do not have
any direct effect on the applicability of the peephole, but they will
be validated afterward, so make sure your constraints are general enough
to apply whenever the peephole matches.  If the peephole matches
but the constraints are not satisfied, the compiler will crash.
</p>
<p>It is safe to omit constraints in all the operands of the peephole; or
you can write constraints which serve as a double-check on the criteria
previously tested.
</p>
<p>Once a sequence of insns matches the patterns, the <var class="var">condition</var> is
checked.  This is a C expression which makes the final decision whether to
perform the optimization (we do so if the expression is nonzero).  If
<var class="var">condition</var> is omitted (in other words, the string is empty) then the
optimization is applied to every sequence of insns that matches the
patterns.
</p>
<p>The defined peephole optimizations are applied after register allocation
is complete.  Therefore, the peephole definition can check which
operands have ended up in which kinds of registers, just by looking at
the operands.
</p>
<a class="index-entry-id" id="index-prev_005factive_005finsn"></a>
<p>The way to refer to the operands in <var class="var">condition</var> is to write
<code class="code">operands[<var class="var">i</var>]</code> for operand number <var class="var">i</var> (as matched by
<code class="code">(match_operand <var class="var">i</var> &hellip;)</code>).  Use the variable <code class="code">insn</code>
to refer to the last of the insns being matched; use
<code class="code">prev_active_insn</code> to find the preceding insns.
</p>
<a class="index-entry-id" id="index-dead_005for_005fset_005fp"></a>
<p>When optimizing computations with intermediate results, you can use
<var class="var">condition</var> to match only when the intermediate results are not used
elsewhere.  Use the C expression <code class="code">dead_or_set_p (<var class="var">insn</var>,
<var class="var">op</var>)</code>, where <var class="var">insn</var> is the insn in which you expect the value
to be used for the last time (from the value of <code class="code">insn</code>, together
with use of <code class="code">prev_nonnote_insn</code>), and <var class="var">op</var> is the intermediate
value (from <code class="code">operands[<var class="var">i</var>]</code>).
</p>
<p>Applying the optimization means replacing the sequence of insns with one
new insn.  The <var class="var">template</var> controls ultimate output of assembler code
for this combined insn.  It works exactly like the template of a
<code class="code">define_insn</code>.  Operand numbers in this template are the same ones
used in matching the original sequence of insns.
</p>
<p>The result of a defined peephole optimizer does not need to match any of
the insn patterns in the machine description; it does not even have an
opportunity to match them.  The peephole optimizer definition itself serves
as the insn pattern to control how the insn is output.
</p>
<p>Defined peephole optimizers are run as assembler code is being output,
so the insns they produce are never combined or rearranged in any way.
</p>
<p>Here is an example, taken from the 68000 machine description:
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_peephole
  [(set (reg:SI 15) (plus:SI (reg:SI 15) (const_int 4)))
   (set (match_operand:DF 0 &quot;register_operand&quot; &quot;=f&quot;)
        (match_operand:DF 1 &quot;register_operand&quot; &quot;ad&quot;))]
  &quot;FP_REG_P (operands[0]) &amp;&amp; ! FP_REG_P (operands[1])&quot;
{
  rtx xoperands[2];
  xoperands[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
#ifdef MOTOROLA
  output_asm_insn (&quot;move.l %1,(sp)&quot;, xoperands);
  output_asm_insn (&quot;move.l %1,-(sp)&quot;, operands);
  return &quot;fmove.d (sp)+,%0&quot;;
#else
  output_asm_insn (&quot;movel %1,sp@&quot;, xoperands);
  output_asm_insn (&quot;movel %1,sp@-&quot;, operands);
  return &quot;fmoved sp@+,%0&quot;;
#endif
})
</pre></div>

<p>The effect of this optimization is to change
</p>
<div class="example smallexample">
<div class="group"><pre class="example-preformatted">jbsr _foobar
addql #4,sp
movel d1,sp@-
movel d0,sp@-
fmoved sp@+,fp0
</pre></div></div>

<p>into
</p>
<div class="example smallexample">
<div class="group"><pre class="example-preformatted">jbsr _foobar
movel d1,sp@
movel d0,sp@-
fmoved sp@+,fp0
</pre></div></div>


<p><var class="var">insn-pattern-1</var> and so on look <em class="emph">almost</em> like the second
operand of <code class="code">define_insn</code>.  There is one important difference: the
second operand of <code class="code">define_insn</code> consists of one or more RTX&rsquo;s
enclosed in square brackets.  Usually, there is only one: then the same
action can be written as an element of a <code class="code">define_peephole</code>.  But
when there are multiple actions in a <code class="code">define_insn</code>, they are
implicitly enclosed in a <code class="code">parallel</code>.  Then you must explicitly
write the <code class="code">parallel</code>, and the square brackets within it, in the
<code class="code">define_peephole</code>.  Thus, if an insn pattern looks like this,
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_insn &quot;divmodsi4&quot;
  [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=d&quot;)
        (div:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0&quot;)
                (match_operand:SI 2 &quot;general_operand&quot; &quot;dmsK&quot;)))
   (set (match_operand:SI 3 &quot;general_operand&quot; &quot;=d&quot;)
        (mod:SI (match_dup 1) (match_dup 2)))]
  &quot;TARGET_68020&quot;
  &quot;divsl%.l %2,%3:%0&quot;)
</pre></div>

<p>then the way to mention this insn in a peephole is as follows:
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_peephole
  [...
   (parallel
    [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=d&quot;)
          (div:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0&quot;)
                  (match_operand:SI 2 &quot;general_operand&quot; &quot;dmsK&quot;)))
     (set (match_operand:SI 3 &quot;general_operand&quot; &quot;=d&quot;)
          (mod:SI (match_dup 1) (match_dup 2)))])
   ...]
  ...)
</pre></div>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="define_005fpeephole2.html">RTL to RTL Peephole Optimizers</a>, Up: <a href="Peephole-Definitions.html">Machine-Specific Peephole Optimizers</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
